package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
@Api(tags = "订单管理接口")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 用户下单
     *
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    @ApiOperation(value = "用户下单接口")
    public R<String> submit(@RequestBody Orders orders) {
        log.info("订单数据：{}", orders);
        orderService.submit(orders);
        return R.success("下单成功");
    }

    /**
     * 订单详情列表
     *
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "订单列表接口")
    public R<Page> showPage(int page, int pageSize, Long number, String beginTime, String endTime) {

        Page<Orders> ordersPage = new Page(page, pageSize);

        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(number != null, Orders::getNumber, number)
                .gt(StringUtils.isNotEmpty(beginTime),Orders::getOrderTime,beginTime)
                .lt(StringUtils.isNotEmpty(endTime),Orders::getOrderTime,endTime);

        orderService.page(ordersPage, queryWrapper);
        return R.success(ordersPage);
    }

    /**
     * 修改订单状态
     *
     * @param ordersDto
     * @return
     */
    @PutMapping
    @ApiOperation(value = "修改订单状态接口")
    public R<String> updataStatus(@RequestBody OrdersDto ordersDto) {

        orderService.updateStatusById(ordersDto);
        return R.success("修改订单状态成功！");
    }

    /**
     * 查看订单
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    @ApiOperation(value = "订单查询接口")
    public R<Page> page(int page, int pageSize) {
        Page pageInfo = new Page<>(page, pageSize);
        return orderService.userPage(pageInfo);
    }

    /**
     * 删除订单
     *
     * @param id
     * @return
     */
    @DeleteMapping
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation(value = "订单删除接口")
    public R<String> deleteOrder(Long id) {
        LambdaQueryWrapper<OrderDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderDetail::getOrderId, id);
        orderDetailService.remove(queryWrapper);
        orderService.removeById(id);
        return R.success("删除成功！");
    }

    /**
     * 再来一单
     *
     * @param orders
     * @return
     */
    @PostMapping("/again")
    @ApiOperation(value = "再来一单接口")
    public R<String> addOrderAgain(@RequestBody Orders orders) {
        if (orders.getId() != null) {
            return R.success("成功！");
        }
        return R.error("失败!");
    }

}
